<?php

namespace App\BO\Order\Trace;

use App\Entities\OrderEntity;
use App\Entities\OrderTraceEntity;

class OrderTraceRecorder
{
    public const BEGIN_MESSAGE = '用户下单';
    public const USER_CANCEL_MESSAGE = '用户取消订单';
    public const SERVICE_FINISHED_MESSAGE = '服务已完成';
    public const USER_APPLY_REFUND_MESSAGE = '用户申请退款';
    public const USER_CANCELLED_REFUND_MESSAGE = '用户取消退款申请';
    public const REFUND_SUCCESS_MESSAGE = '订单已退款';
    public const PAY_SUCCESS_MESSAGE = '订单已支付';
    public const PAY_EXPIRED_MESSAGE = '订单过期末支付';
    public const DISTRIBUTED_MESSAGE = '订单已派单';
    public const COMMENTED_TRACE_MESSAGE = '订单已评价';

    public const REFUND_AGREE_TRACE_MESSAGE = '退单通过';

    public const IMPORT_PAID_ORDER_MESSAGE = '后台导入已经支付订单';
    public const IMPORT_WAITING_PAY_ORDER_MESSAGE = '后台导入末支付订单';
    public const IMPORT_PAID_AND_FINISHED_ORDER_MESSAGE = '后台导入已完成已支付订单';
    public const IMPORT_NOT_PAID_AND_FINISHED_ORDER_MESSAGE = '后台导入已完成末付订单';

    public static function addImportPaidOrderTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::IMPORT_PAID_ORDER_MESSAGE);
    }

    public static function addImportPaidAndFinishedOrderTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::IMPORT_PAID_AND_FINISHED_ORDER_MESSAGE);
    }

    public static function addImportNotPaidAndFinishedOrderTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::IMPORT_NOT_PAID_AND_FINISHED_ORDER_MESSAGE);
    }

    public static function addImportWaitingPayOrderTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::IMPORT_WAITING_PAY_ORDER_MESSAGE);
    }

    public static function addBeginTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::BEGIN_MESSAGE);
    }

    public static function addUserCancelTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::USER_CANCEL_MESSAGE);
    }

    public static function addUserApplyRefundTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::USER_APPLY_REFUND_MESSAGE);
    }

    public static function addUserCancelledRefundRefundTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::USER_CANCELLED_REFUND_MESSAGE);
    }

    public static function addServiceFinishedTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::SERVICE_FINISHED_MESSAGE);
    }

    public static function addRefundSuccessTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::REFUND_SUCCESS_MESSAGE);
    }

    public static function addPaySuccessTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::PAY_SUCCESS_MESSAGE);
    }

    public static function addPayExpiredTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::PAY_EXPIRED_MESSAGE);
    }

    public static function addDistributedTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::DISTRIBUTED_MESSAGE);
    }

    public static function addCommentedTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::COMMENTED_TRACE_MESSAGE);
    }

    public static function addRefundAgreeTrace(OrderEntity $orderEntity)
    {
        self::save($orderEntity, self::REFUND_AGREE_TRACE_MESSAGE);
    }

    /**
     * @param OrderEntity $order
     * @param string $message
     * @return void
     */
    private static function save(OrderEntity $order, string $message)
    {
        $orderTrace = OrderTraceEntity::getNewInstance();

        $orderTrace->setOrderId($order->getId());
        $orderTrace->setOrderStateDesc($message);
        $orderTrace->setOrderState($order->getOrderState());
        $orderTrace->save();
    }
}
